<?php

namespace app\api\controller\user;

use app\api\controller\Controller;
use app\api\model\dealer\Setting;
use app\api\model\User;
use app\api\model\User as UserModel;
use app\api\model\Order as OrderModel;
use app\api\model\Setting as SettingModel;
use app\common\model\sharing\UserRedBag;

/**
 * 个人中心主页
 * Class Index
 * @package app\api\controller\user
 */
class Index extends Controller
{
    /**
     * 获取当前用户信息
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        // 当前用户信息
        $user = $this->getUser(false);
        // 订单总数
        $model = new OrderModel;

        // LLH 待领取红包
        if($user){
            $RedBag = (new UserRedBag());
            $user['prepare_red_bag'] = $RedBag
                ->where("user_id",$user['user_id'])
                ->where("status",1)
                ->sum("award_money");

            $user['succeed_red_bag'] = $RedBag
                ->where("user_id",$user['user_id'])
                ->where("status",2)
                ->sum("award_money");

            $user['withdraw_money'] = (new \app\common\model\sharing\Withdraw())
                ->where("user_id",$user['user_id'])
                ->where("apply_status",40)
                ->sum("money");
        }


        return $this->renderSuccess([
            'userInfo' => $user,
            'orderCount' => [
                'payment' => $model->getCount($user, 'payment'),
                'received' => $model->getCount($user, 'received'),
                'comment' => $model->getCount($user, 'comment'),
            ],
            'setting' => [
                'points_name' => SettingModel::getPointsName(),
            ],
            'menus' => (new UserModel)->getMenus()   // 个人中心菜单列表
        ]);
    }


    /**
     * 分销商提现信息
     * @return array
     */
    public function withdraw()
    {
        $user = $this->getUser(false);
        return $this->renderSuccess([
            'background' => base_url() . 'assets/api/dealer-bg.png',
            'money' =>  (new User())->where("user_id",$user['user_id'])->value("balance"),
        ]);
    }

}
